package com.prawn.ift_collequipment.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.prawn.ift_collequipment.dao.EquipmentBreakdownDao;
import com.prawn.ift_collequipment.pojo.EquipmentBreakdown;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class EquipmentBreakdownService {

	@Autowired
	private EquipmentBreakdownDao equipmentBreakdownDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<EquipmentBreakdown> findAll() {
		return equipmentBreakdownDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<EquipmentBreakdown> findSearch(Map whereMap, int page, int size) {
		Specification<EquipmentBreakdown> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return equipmentBreakdownDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<EquipmentBreakdown> findSearch(Map whereMap) {
		Specification<EquipmentBreakdown> specification = createSpecification(whereMap);
		return equipmentBreakdownDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	@Cacheable(value = "equipmentBreakdown",key = "#id")
	public EquipmentBreakdown findById(String id) {
		return equipmentBreakdownDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param equipmentBreakdown
	 */
	public void add(EquipmentBreakdown equipmentBreakdown) {
		equipmentBreakdown.setId( idWorker.nextId()+"" );
		equipmentBreakdownDao.save(equipmentBreakdown);
	}

	/**
	 * 修改
	 * @param equipmentBreakdown
	 */
	@CacheEvict(value = "equipmentBreakdown",key = "#equipmentBreakdown.id")
	public void update(EquipmentBreakdown equipmentBreakdown) {
		equipmentBreakdownDao.save(equipmentBreakdown);
	}

	/**
	 * 删除
	 * @param id
	 */
	@CacheEvict(value = "equipmentBreakdown",key = "#id")
	public void deleteById(String id) {
		equipmentBreakdownDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<EquipmentBreakdown> createSpecification(Map searchMap) {

		return new Specification<EquipmentBreakdown>() {

			@Override
			public Predicate toPredicate(Root<EquipmentBreakdown> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // ID
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 引用控制设备编号
                if (searchMap.get("equipmentId")!=null && !"".equals(searchMap.get("equipmentId"))) {
                	predicateList.add(cb.like(root.get("equipmentId").as(String.class), "%"+(String)searchMap.get("equipmentId")+"%"));
                }
                // 所处场景
                if (searchMap.get("sceneId")!=null && !"".equals(searchMap.get("sceneId"))) {
                	predicateList.add(cb.like(root.get("sceneId").as(String.class), "%"+(String)searchMap.get("sceneId")+"%"));
                }
                // 设备类型：0智能；1控制
                if (searchMap.get("eqType")!=null && !"".equals(searchMap.get("eqType"))) {
                	predicateList.add(cb.like(root.get("eqType").as(String.class), "%"+(String)searchMap.get("eqType")+"%"));
                }
                // 故障类型
                if (searchMap.get("bdType")!=null && !"".equals(searchMap.get("bdType"))) {
                	predicateList.add(cb.like(root.get("bdType").as(String.class), "%"+(String)searchMap.get("bdType")+"%"));
                }
                // 故障症状
                if (searchMap.get("symptom")!=null && !"".equals(searchMap.get("symptom"))) {
                	predicateList.add(cb.like(root.get("symptom").as(String.class), "%"+(String)searchMap.get("symptom")+"%"));
                }
                // 故障等级
                if (searchMap.get("bdClass")!=null && !"".equals(searchMap.get("bdClass"))) {
                	predicateList.add(cb.like(root.get("bdClass").as(String.class), "%"+(String)searchMap.get("bdClass")+"%"));
                }
                // 故障原因
                if (searchMap.get("happenReason")!=null && !"".equals(searchMap.get("happenReason"))) {
                	predicateList.add(cb.like(root.get("happenReason").as(String.class), "%"+(String)searchMap.get("happenReason")+"%"));
                }
                // 解决故障方法
                if (searchMap.get("resolveWay")!=null && !"".equals(searchMap.get("resolveWay"))) {
                	predicateList.add(cb.like(root.get("resolveWay").as(String.class), "%"+(String)searchMap.get("resolveWay")+"%"));
                }
                // 故障描述
                if (searchMap.get("bdDescription")!=null && !"".equals(searchMap.get("bdDescription"))) {
                	predicateList.add(cb.like(root.get("bdDescription").as(String.class), "%"+(String)searchMap.get("bdDescription")+"%"));
                }
                // 引用通道ID
                if (searchMap.get("channelId")!=null && !"".equals(searchMap.get("channelId"))) {
                	predicateList.add(cb.like(root.get("channelId").as(String.class), "%"+(String)searchMap.get("channelId")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                	predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 删除标记
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                	predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
